﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using KOPLibrary.TemplateManagement;
using KOPLibrary.Forms;
using KOPLibrary;
using KOPLibrary.PreferenceManagement;
using System.IO;
using KOPLibrary.UpdateService;
using System.Data.SqlTypes;
using System.Reflection;
using System.Windows.Forms;
using System.Diagnostics;
using KOPLibrary.Exceptions;
using System.Drawing;
using Microsoft.Office.Interop.Excel;
using KOPLibrary.DataManagement;
using KOPLibrary.DataManagement.Cacheflow;

namespace KOP.Excel
{
    public partial class KOPExcelRibbon : OfficeRibbon
    {
        public KOPExcelRibbon()
        {
            InitializeComponent();
        }

        private void KOPRibbon_Load(object sender, RibbonUIEventArgs e)
        {

        }

        private void gallery1_Click(object sender, RibbonControlEventArgs e)
        {
            
            if (gallery1.SelectedItem != null)
            {
                OpenTemplate(gallery1.SelectedItem.Tag.ToString());
            }
        }

        private void gallery1_ItemsLoading(object sender, RibbonControlEventArgs e)
        {
            if (gallery1.Items.Count == 0)
            {
                TemplateManager manager = new TemplateManager(KOPLibrary.TemplateManagement.TemplateType.Excel, "");
                manager.RefreshGalleryItems(gallery1);
            }
        }

        private void gallery1_ButtonClick(object sender, RibbonControlEventArgs e)
        {
            if (e.Control.Id == "ctlShowAllTemplates")
            {
                TemplateBrowserForm form = TemplateBrowserForm.Create(KOPLibrary.TemplateManagement.TemplateType.Excel, "");
                string selectedTemplate = form.Execute();
                if (!string.IsNullOrEmpty(selectedTemplate))
                {
                    OpenTemplate(selectedTemplate);
                }
            }
            else if (e.Control.Id == "ctlUpdateTemplates")
            {
                Util.UpdateTemplates(KOPLibrary.TemplateManagement.TemplateType.Excel);
            }
            else if (e.Control.Id == "ctlTemplateLibraryPreference")
            {
                PreferenceForm form = new PreferenceForm(KOPLibrary.TemplateManagement.TemplateType.Excel);
                ((TabControl)form.Controls["ctlTabControl"]).SelectedIndex = 0;
                form.ShowDialog();
            }
        }

        private void OpenTemplate(string template)
        {
            TemplateManager tmanager = new TemplateManager(KOPLibrary.TemplateManagement.TemplateType.Excel, "");
            tmanager.AddToRecentlyUsedList(template);
            tmanager.RefreshGalleryItems(gallery1);
            ExcelManager manager = new ExcelManager(Globals.ExcelAddIn.Application);
            manager.CreateExcelWorkbook(gallery1.SelectedItem.Tag.ToString());
        }

        private void ctlAboutButton_Click(object sender, RibbonControlEventArgs e)
        {
            About form = new About();
            form.ShowDialog();
        }

        private void ctlPreferenceButton_Click(object sender, RibbonControlEventArgs e)
        {
            PreferenceForm form = new PreferenceForm(KOPLibrary.TemplateManagement.TemplateType.Excel);
            form.ShowDialog();
        }

        private void ctlVersionControl_Click(object sender, RibbonControlEventArgs e)
        {
            Util.UpdateAddin();
        }

        private void ctlSupport_Click(object sender, RibbonControlEventArgs e)
        {
            Util.GoToSupport();
        }

        private void button1_Click(object sender, RibbonControlEventArgs e)
        {
            Util.OpenDocumentation();
        }

        private void ctlWordEntegration_Click(object sender, RibbonControlEventArgs e)
        {
            ExcelManager manager = new ExcelManager(Globals.ExcelAddIn.Application);
            AbstractData data = SelectExcelDataForm.Execute(manager, null);
            if ((string)ctlWordEntegration.SelectedItem.Tag == "ctlWordBinding")
            {
                if (data != null)
                {
                    BindWordTemplateForm form = new BindWordTemplateForm(data);
                    form.ShowDialog();
                }
            }
            if ((string)ctlWordEntegration.SelectedItem.Tag == "ctlCreateWordForm")
            {
                if (data != null)
                {
                    TemplateDesigner form = new TemplateDesigner(null, KOPLibrary.TemplateManagement.TemplateType.Excel, data);
                    form.ShowDialog();
                }
            }

        }

        private void ctlEMail_Click(object sender, RibbonControlEventArgs e)
        {
            ExcelManager manager = new ExcelManager(Globals.ExcelAddIn.Application);
            AbstractData data = SelectExcelDataForm.Execute(manager, null);
            if (data != null)
            {
                eMailForm form = new eMailForm();
                form.Execute(data, KOPLibrary.TemplateManagement.TemplateType.Excel);
            }
        }

        private void ctlEnvlope_Click(object sender, RibbonControlEventArgs e)
        {
            ExcelManager manager = new ExcelManager(Globals.ExcelAddIn.Application);
            AbstractData data = SelectExcelDataForm.Execute(manager, null);
            if (data != null)
            {
                new LabelForm().Execute(data);
            }
        }

        private void ctlSecureDocumentBtn_Click(object sender, RibbonControlEventArgs e)
        {
            if ((string)ctlSecureDocumentBtn.SelectedItem.Tag == "ctlSecureDocument")
            {
                string pwd = ProtectDocument.ExecuteProtectDocument(KOPLibrary.TemplateManagement.TemplateType.Excel);
                if (!string.IsNullOrEmpty(pwd))
                {
                    Globals.ExcelAddIn.Application.ActiveWorkbook.Password = pwd;
                    if (Util.Confirm("Dokümanı kaydetmek istiyor musunuz ?"))
                        Globals.ExcelAddIn.Application.ActiveWorkbook.Save();
                }
            }
            else if ((string)ctlSecureDocumentBtn.SelectedItem.Tag == "ctlRemovePersonalInfo")
            {
                Globals.ExcelAddIn.Application.DisplayDocumentInformationPanel = true;
                if (MessageBox.Show("Doküman başlık ve kişisel bilgiler temizlemek istiyor musunuz ?", "Onayla", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
                {
                    Globals.ExcelAddIn.Application.ActiveWorkbook.RemoveDocumentInformation(Microsoft.Office.Interop.Excel.XlRemoveDocInfoType.xlRDIAll);
                    MessageBox.Show("Doküman başlık ve kişisel bilgiler temizlendi. Dokümanı kaydettikten sonra güvenle başkalarıyla paylaşabilirsiniz.");
                }
            }
        }

        private void ctlOpenSecuredDocument_Click(object sender, RibbonControlEventArgs e)
        {
            if (e.Control.Id == "ctlOpenSecuredDocument")
            {
                OpenProtectedDocument.OpenProtectDocumentResult res = OpenProtectedDocument.ExecuteOpenProtectDocument(KOPLibrary.TemplateManagement.TemplateType.Excel);
                if (res != null)
                {
                    ExcelManager mng = new ExcelManager(Globals.ExcelAddIn.Application);
                    mng.OpenDocument(res.DocumentPath, res.DocPwd);
                }
            }
        }

        private void button2_Click(object sender, RibbonControlEventArgs e)
        {

        }

        private void ctlcashflowGallery_Click(object sender, RibbonControlEventArgs e)
        {
            if ((string)ctlcashflowGallery.SelectedItem.Tag == "ctlCashData")
            {
                KOPLibrary.Forms.CashFlow.MainCashflowForm form = new KOPLibrary.Forms.CashFlow.MainCashflowForm();
                form.Execute(Globals.ExcelAddIn.Application);
            }
            else if ((string)ctlcashflowGallery.SelectedItem.Tag == "ctlCashAnalyse")
            {
                global::KOPLibrary.Cacheflow.CacheflowDs ds = DataLayer.LoadCachflowData();
                if (!KOPLibrary.Forms.CashFlow.MainCashflowForm.CheckIfCreateSampleData(ds))
                {
                    if (ds.Ana_Hesap.Count > 0 || ds.Hesap.Count > 0 || ds.Hareket.Count > 0)
                    {
                        Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                        ExcelExporter exporter = new ExcelExporter(app, ExportType.CreateNew);
                        try
                        {
                            exporter.CreateAnalyse(ds.Hareket.DefaultView);
                        }
                        finally
                        {
                            app.Visible = true;
                        }
                    }
                }
            }
            else if ((string)ctlcashflowGallery.SelectedItem.Tag == "ctlRefresh")
            {
                try
                {
                    Globals.ExcelAddIn.Application.ActiveWorkbook.Sheets.get_Item(ExcelExporter.DataSheetName);
                    global::KOPLibrary.Cacheflow.CacheflowDs ds = DataLayer.LoadCachflowData();
                    if (ds.Ana_Hesap.Count > 0 || ds.Hesap.Count > 0 || ds.Hareket.Count > 0)
                    {
                        ExcelExporter exporter = new ExcelExporter(Globals.ExcelAddIn.Application, ExportType.UpdateExsiting);
                        
                        exporter.CreateAnalyse(ds.Hareket.DefaultView);
                    }
                }
                catch
                {
                    if (Util.Confirm("Mevcut çalışma dosyası nakit akış analiz ortamına benzemiyor. Yeni bir analiz ortamı oluşturmak ister misiniz ?"))
                    {
                        global::KOPLibrary.Cacheflow.CacheflowDs ds = DataLayer.LoadCachflowData();
                        if (!KOPLibrary.Forms.CashFlow.MainCashflowForm.CheckIfCreateSampleData(ds))
                        {
                            if (ds.Ana_Hesap.Count > 0 || ds.Hesap.Count > 0 || ds.Hareket.Count > 0)
                            {
                                Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                                ExcelExporter exporter = new ExcelExporter(app, ExportType.CreateNew);
                                try
                                {
                                    exporter.CreateAnalyse(ds.Hareket.DefaultView);
                                }
                                finally
                                {
                                    app.Visible = true;
                                }
                            }
                        }
                    }
                }
            }

            else
            {
                global::KOPLibrary.Cacheflow.CacheflowDs ds = DataLayer.LoadCachflowData();
                if (!KOPLibrary.Forms.CashFlow.MainCashflowForm.CheckIfCreateSampleData(ds))
                {
                    if (ds.Ana_Hesap.Count > 0 || ds.Hesap.Count > 0 || ds.Hareket.Count > 0)
                    {
                        ExcelExporter exporter = new ExcelExporter(Globals.ExcelAddIn.Application, ExportType.UpdateExsiting);
                        exporter.CreateAnalyse(ds.Hareket.DefaultView);
                    }
                }
            }
        }


        private void ctlcashflowGallery_ItemsLoading(object sender, RibbonControlEventArgs e)
        {
            RibbonDropDownItem item = ctlcashflowGallery.Items.Single(p => (string)p.Tag == "ctlRefresh");
        }

        private void ctlcashflowGallery_ButtonClick(object sender, RibbonControlEventArgs e)
        {
            if (e.Control.Id == "ctlCashflowOptions")
            {
                PreferenceForm form = new PreferenceForm(KOPLibrary.TemplateManagement.TemplateType.Excel);
                form.ShowDialog();
            }
        }


    }
}
